package com.healthai.service;

import com.healthai.dto.request.DailyRecordRequest;
import com.healthai.entity.DailyRecord;
import com.healthai.exception.BusinessException;
import com.healthai.mapper.DailyRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class DailyRecordService {
    
    @Autowired
    private DailyRecordMapper dailyRecordMapper;
    
    /**
     * 创建或更新每日记录
     * @param userId 用户ID
     * @param request 每日记录请求
     * @return 记录ID
     */
    public Integer saveDailyRecord(Integer userId, DailyRecordRequest request) {
        System.out.println("nihao");
        // 查询是否已有该日期的记录
        DailyRecord existingRecord = dailyRecordMapper.findByUserIdAndDate(userId, request.getRecordDate());
        
        DailyRecord record = new DailyRecord();
        BeanUtils.copyProperties(request, record);
        record.setUserId(userId);
        
        if (existingRecord != null) {
            // 更新现有记录
            record.setRecordId(existingRecord.getRecordId());
            record.setUpdateTime(LocalDateTime.now());
            dailyRecordMapper.update(record);
            return existingRecord.getRecordId();
        } else {
            // 创建新记录
            record.setCreateTime(LocalDateTime.now());
            record.setUpdateTime(LocalDateTime.now());
            dailyRecordMapper.insert(record);
            return record.getRecordId();
        }
    }
    
    /**
     * 获取用户某日期的每日记录
     * @param userId 用户ID
     * @param date 日期
     * @return 每日记录
     */
    public DailyRecord getDailyRecordByDate(Integer userId, LocalDate date) {
        DailyRecord record = dailyRecordMapper.findByUserIdAndDate(userId, date);
//        if (record == null) {
//            throw new BusinessException(404, "未找到该日期的记录");
//        }
        return record;
    }
    
    /**
     * 获取用户日期范围内的每日记录
     * @param userId 用户ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 每日记录列表
     */
    public List<DailyRecord> getDailyRecordsByDateRange(Integer userId, LocalDate startDate, LocalDate endDate) {
        return dailyRecordMapper.findByUserIdAndDateRange(userId, startDate, endDate);
    }
    
    /**
     * 获取用户最近的每日记录
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 每日记录列表
     */
    public List<DailyRecord> getRecentDailyRecords(Integer userId, Integer limit) {
        return dailyRecordMapper.findRecentByUserId(userId, limit);
    }
    
    /**
     * 删除每日记录
     * @param userId 用户ID
     * @param recordId 记录ID
     * @return 是否成功
     */
    public boolean deleteDailyRecord(Integer userId, Integer recordId) {
        // 检查记录是否存在且属于该用户
        DailyRecord record = dailyRecordMapper.findById(recordId);
        if (record == null || !record.getUserId().equals(userId)) {
            throw new BusinessException(404, "记录不存在或无权限删除");
        }
        
        return dailyRecordMapper.deleteById(recordId) > 0;
    }
}